package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Goods;
import com.example.demo.entity.User;
import com.example.demo.model.PageModel;
import com.example.demo.model.Result;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    /*商品列表数据*/
    @GetMapping("")
    public Result<PageModel<Goods>> getGoodsList(@RequestParam(value = "query",required = false,defaultValue = "") String query,
                                  @RequestParam("pagenum") Integer pagenum,
                                  @RequestParam("pagesize") Integer pagesize){
        IPage<Goods> ipage = new Page<>(pagenum,pagesize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("goods_name",query);
        IPage<Goods> page = goodsService.page(ipage, queryWrapper);
        PageModel<Goods> goodsResult = new PageModel<>(page.getTotal(),page.getCurrent(),page.getRecords());
        return new Result<>(200,"获取成功",goodsResult);
    }
    /*添加商品*/
    @PostMapping("")
    public Result<Goods> addGoods(@RequestParam("goods_name") String goods_name,
                                  @RequestParam("goods_cat") String goods_cat,
                                  @RequestParam("goods_price") BigDecimal goods_price,
                                  @RequestParam("goods_number") Integer goods_number,
                                  @RequestParam("goods_weight") Integer goods_weight,
                                  @RequestParam(value = "goods_introduce",required = false,defaultValue = "") String goods_introduce){
        Goods goods = new Goods();
        goods.setGoodsName(goods_name);
        goods.setGoodsPrice(goods_price);
        goods.setGoodsNumber(goods_number);
        goods.setGoodsWeight(goods_weight);
        goods.setGoodsIntroduce(goods_introduce);
        boolean save = goodsService.save(goods);
        if(save){
            return new Result<>(201,"创建商品成功",goods);
        }
        return new Result<>(0,"创建商品失败",null);
    }
    /*根据 ID 查询商品*/

}

